#ifndef IAPCONNECTIONMANAGER_H_
#define IAPCONNECTIONMANAGER_H_

#include <Es_sock.h> //,RConnection ,RSocketServ
#include <http\rhttpconnectioninfo.h> //RHTTPConnectionInfo
#include <http\RHTTPSession.h>
#include <rconnmon.h>
#include "CActiveHelper.h"

struct MIapConnectionManagerCallback
	{
	virtual void MIapConnectionManagerCallbackRunL(TInt iStage, TInt iError) = 0;
	virtual ~MIapConnectionManagerCallback();
	};


class CIapConnectionManager : public MActiveHelperCallback<RConnection*>, public MActiveHelperCallback<RConnectionMonitor*>, public MActiveHelperCallback<TNifProgressBuf*>, public MConnectionMonitorObserver
	{
public:
	static CIapConnectionManager* NewL();
	static CIapConnectionManager* NewLC();
	
	RHTTPSession iSession;
	RSocketServ iSockServ;
	RConnection iConnection;
	CActiveHelper<RConnection*>* iConnectionActiveHelper;
	void MActiveHelperCallbackRunL(CActiveHelper<RConnection*>* aActiveHelper, RConnection* aUserData);
	TInt MActiveHelperCallbackRunError(CActiveHelper<RConnection*>* aActiveHelper, RConnection* aUserData, TInt aError);
	void MActiveHelperCallbackDoCancel(CActiveHelper<RConnection*>* aActiveHelper, RConnection* aUserData);

	TNifProgressBuf iProgBuf;
	CActiveHelper<TNifProgressBuf*>* iConnectionProgressNotificationActiveHelper;
	void MActiveHelperCallbackRunL(CActiveHelper<TNifProgressBuf*>* aActiveHelper, TNifProgressBuf* aUserData);
	TInt MActiveHelperCallbackRunError(CActiveHelper<TNifProgressBuf*>* aActiveHelper, TNifProgressBuf* aUserData, TInt aError);
	void MActiveHelperCallbackDoCancel(CActiveHelper<TNifProgressBuf*>* aActiveHelper, TNifProgressBuf* aUserData);
	
	void SetIapPriorityList(RArray<TUint32>* aIapPriorityList);
	RArray<TUint32>* iIapPriorityList;	
	TInt iIapPriorityListCurrentIndex;
	RArray< TBuf<100> > iNearbyWlanNetworkNames;
	
	RArray<MIapConnectionManagerCallback*> iSubscribersList;
	void SubscribeL(MIapConnectionManagerCallback* aTarget);
	void UnSubscribeL(MIapConnectionManagerCallback* aTarget);
	void NotifySubscribers(TInt iStage, TInt iError);

	RConnectionMonitor iConnectionMonitor;
	TPckgBuf<TConnMonNetworkNames> iPkgNetworks;
	CActiveHelper<RConnectionMonitor*>* iActiveConnectionMonitor;
	void MActiveHelperCallbackRunL(CActiveHelper<RConnectionMonitor*>* aActiveHelper, RConnectionMonitor* aUserData);
	TInt MActiveHelperCallbackRunError(CActiveHelper<RConnectionMonitor*>* aActiveHelper, RConnectionMonitor* aUserData, TInt aError);
	void MActiveHelperCallbackDoCancel(CActiveHelper<RConnectionMonitor*>* aActiveHelper, RConnectionMonitor* aUserData);
	void EventL (const CConnMonEventBase &aConnMonEvent);

	void ConnectL(MIapConnectionManagerCallback* aTarget);
	TBool IsConnecting();
	void ResetConnectionL();
	static TInt SaveIapPriorityL(const RArray<TUint32>& aIapPriorityList, const TDesC& aFileName);
	static TInt LoadIapPriorityL(RArray<TUint32>& aIapPriorityList, const TDesC& aFileName);
	virtual ~CIapConnectionManager();
private:
	void ConstructL();
	TInt TryNextIap();
	CIapConnectionManager();
	};

#endif /* IAPCONNECTIONMANAGER_H_ */
